<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>受控组件</title>
  <!--  引入React核心库-->
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <!--  引入react-dom,用于支持React操作Dom-->
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <!-- 引入babel，用于将jsx转为js-->
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  <script src="https://unpkg.com/prop-types@15.6.2/prop-types.js"></script>
</head>

<body>
  <!-- 准备一个容器 -->
  <div id="root"></div>
  <script type="text/babel">

    // 1.创建一个组件
    class Login extends React.Component {
      state = {
        username: '',
        password: ''
      }
      // 保存用户名到状态中
      saveUsername = (event) => {
        this.setState({ username: event.target.value })
      }
      // 保存密码到状态中
      savePassword = (event) => {
        this.setState({ password: event.target.value })
      }
      // 表单提交
      handleSubmit = (event) => {
        console.log(this.state);
        event.preventDefault() // 阻止表单默认提交
        const { username, password } = this.state
        alert(`你的用户名是:${username},你输入的密码是：${password}`)
      }
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            用户名：<input onChange={this.saveUsername} type="text" placeholder='请输入用户名' name='username' />&nbsp;
            密码 <input onChange={this.savePassword} type="password" placeholder='请输入密码' name='password' />&nbsp;
            <button>登录</button>
          </form>
        )
      }
    }

    // 2.渲染组件到页面
    const container = document.getElementById('root')
    const root = ReactDOM.createRoot(container)
    root.render(<Login />)
  </script>
</body>

</html>